#!/usr/bin/python
#    DMnapi for Dreambox-Enigma2
#    Version: 13.1.30
#    Coded by areq (c) 2010-2013
#    Support: http://dvhk.pl/
#    http://code.google.com/p/dmnapi/

VERSION="13.1.30"
DM_patch = { "FileBrowser": [[[
"""
					self.SetGB(_("Play"))
					self.SetRB(_("Information"))
""",
"""
					self.SetGB(_("Play"))
					self.SetRB(_("Get Subtitle"))		#dmnapi
"""],[
"""
			elif sel[B_FILTER] & (FILTER_PICS | FILTER_MOVIES | FILTER_SOUND):
				from searchMediaInfo import BrowserFileInfos
				self.session.open(BrowserFileInfos, sel)
""",
"""
			elif sel[B_FILTER] & (FILTER_PICS | FILTER_SOUND):
				from searchMediaInfo import BrowserFileInfos
				self.session.open(BrowserFileInfos, sel)
			elif sel[B_FILTER] & FILTER_MOVIES:
				from Plugins.Extensions.DMnapi.DMnapi import DMnapi
				self.session.openWithCallback(self.__movie_Callback, DMnapi, sel[B_FULL])
"""]],[[   # druga werja
"""
				elif sel[3][B_FILTER] & FILTER_MOVIES:
					self.SetGB(_("Play"))
					if pathExists(sel[3][B_CFILE] + ".txt"):
						self.SetRB(_("Information"))
""",
"""
				elif sel[3][B_FILTER] & FILTER_MOVIES:
					self.SetGB(_("Play"))
					self.SetRB(_("Get Subtitle"))		#dmnapi
"""],[
"""
			elif sel[B_FILTER] & FILTER_MOVIES:
				from confFileBrowser import BrowserFileInfos
				self.session.open(BrowserFileInfos, sel)
""",
"""
			elif sel[B_FILTER] & FILTER_MOVIES:
				from Plugins.Extensions.DMnapi.DMnapi import DMnapi
				self.session.openWithCallback(self.__movie_Callback, DMnapi, sel[B_FULL])
"""]],[[ # trzecia werja
"""
					self.SetGB(_("Show"))
				elif sel[3][B_FILTER] & FILTER_MOVIES:
					self.SetGB(_("Play"))
				elif sel[3][B_FILTER] & FILTER_TS:
					self.SetGB(_("Play"))
					self.SetRB(_("Information"))
""",
"""
					self.SetGB(_("Show"))
				elif sel[3][B_FILTER] & FILTER_MOVIES:
					self.SetGB(_("Play"))
					self.SetRB(_("Get Subtitle"))			#dmnapi
				elif sel[3][B_FILTER] & FILTER_TS:
					self.SetGB(_("Play"))
					self.SetRB(_("Information"))
"""],[
"""
				from Plugins.Extensions.PicturePlayer.plugin import Pic_Exif
				self.session.open(Pic_Exif, picload.getInfo(sel[B_FULL]))
				del picload
		except:
			pass

#----------------------------Gelb-Favo
	def KeyBlue(self):
		if self.filter == FILTER_NONE:
""",
"""
				from Plugins.Extensions.PicturePlayer.plugin import Pic_Exif
				self.session.open(Pic_Exif, picload.getInfo(sel[B_FULL]))
				del picload
			elif sel[B_FILTER] & FILTER_MOVIES:							#dmnapi
				from Plugins.Extensions.DMnapi.DMnapi import DMnapi				#dmnapi
				self.session.openWithCallback(self.__napiCallback, DMnapi, sel[B_FULL])		#dmnapi
		except:
			pass

	def __napiCallback(self, val = False): 				#dmnapi
		self.__fillListe()  					#dmnapi

#----------------------------Gelb-Favo
	def KeyBlue(self):
		if self.filter == FILTER_NONE:
"""]],],
"BPBrowser": [[[
"""
					self["key_green"].setText(_("Show"))
				elif sel[3][B_FILTER] & Cbputils.FILTER_MOVIES:
					self["key_green"].setText(_("Play"))
				elif sel[3][B_FILTER] & Cbputils.FILTER_TS:
					self["key_green"].setText(_("Play"))
					self["key_red"].setText(_("Info"))
""",
"""
					self["key_green"].setText(_("Show"))
				elif sel[3][B_FILTER] & Cbputils.FILTER_MOVIES:
					self["key_green"].setText(_("Play"))
					self["key_red"].setText(_("Get Subtitle"))	#dmnapi
				elif sel[3][B_FILTER] & Cbputils.FILTER_TS:
					self["key_green"].setText(_("Play"))
					self["key_red"].setText(_("Info"))
"""],[
"""
			from Plugins.Extensions.PicturePlayer.plugin import Pic_Exif
			self.session.open(Pic_Exif, picload.getInfo(sel[B_FULL]))
			del picload

#----------------------------Gelb-Favo
	def KeyYellow(self):
""",
"""
			from Plugins.Extensions.PicturePlayer.plugin import Pic_Exif
			self.session.open(Pic_Exif, picload.getInfo(sel[B_FULL]))
			del picload
		elif sel[B_FILTER] & Cbputils.FILTER_MOVIES:		#dmnapi
			from Plugins.Extensions.DMnapi.DMnapi import DMnapi		#dmnapi
			self.session.openWithCallback(self.__napiCallback, DMnapi, sel[B_FULL])		#dmnapi

	def __napiCallback(self, val = False):		#dmnapi
		self.fill_liste() 		#dmnapi

#----------------------------Gelb-Favo
	def KeyYellow(self):
"""]]],
"plugin": [[[
"""
			"showMovies": self.CloseAndPlay
		}, -1)
		self.onLayoutFinish.append(self.byLayoutEnd)

	def ok(self):
		global DVDPlayerAviable
""",
"""
			"showMovies": self.CloseAndPlay,
			"dmnapi": self.DMnapi			#dmnapi
		}, -1)
		self.onLayoutFinish.append(self.byLayoutEnd)
	
	def DMnapi(self):		#dmnapi
		if not self["filelist"].canDescent():		#dmnapi
			curSelFile = self["filelist"].getCurrentDirectory() + self["filelist"].getFilename()	#dmnapi
			from Plugins.Extensions.DMnapi.DMnapi import DMnapi		#dmnapi
			self.session.openWithCallback(self.dmnapiCallback, DMnapi, curSelFile)		#dmnapi
						
	def dmnapiCallback(self, answer=False):		#dmnapi
		self["filelist"].refresh()		#dmnapi

	def ok(self):
		global DVDPlayerAviable
"""]
]],
"kolejny": [[[
"""co""",
"""nco"""],[
"""co""",
"""naco"""]]]
}

def saveDEkeymap():
    q="""<keymap>
	<map context="ColorActions">
		<key id="KEY_TEXT" mapto="dmnapi" flags="m" />
	</map>
</keymap>
"""
    open("/usr/lib/enigma2/python/Plugins/Extensions/DreamExplorer/keymap.xml", 'w').write(q)

def dmnapi_chceck():
    if FB.find('#dmnapi') > 0:
	return "Instaled"
    else:
	return "NOT Instaled"

def patch(fn, FB):
    fn = fn.split('/')[-1].split('.')[0]
    for sub in DM_patch.get(fn,[]):
	for s,r in sub:
	    if FB.find(s) < 0:
		break
	else:
	    for s,r in sub:
		FB = FB.replace(s, r)
    return FB

if __name__ == "__main__":
	import sys, re, os.path

	print "DMnapi installer.\nVersion: %s by areq.\n" % VERSION
	for d, f, v, reg in [ \
	('Gemini3 FileBrowser', '/usr/lib/enigma2/python/Plugins/Bp/geminimain/FileBrowser.py','/usr/lib/enigma2/python/Plugins/Bp/geminimain/gVersion.py', "gVersion = '(.*)'"),\
	('Gemini2 BPBrowser','/usr/lib/enigma2/python/Bp/BPBrowser.py','/etc/issue.net', "OpenDreambox (.*) %h"),\
	('DreamExplorer','/usr/lib/enigma2/python/Plugins/Extensions/DreamExplorer/plugin.py','/usr/lib/enigma2/python/Plugins/Extensions/DreamExplorer/plugin.py', "Version: (.*)") ]:
		if os.path.exists(f) and os.path.exists(v):
			q = open(v).read()
			try:
				ver = re.search(reg , q).group(1)
			except:
				ver = 'unknown'
			print d,"\n\tVersion:", ver,
			FB = open(f).read()
			if FB.find("Plugins.Extensions.DMnapi.plugin") > 0:
				FBp = FB.replace("Plugins.Extensions.DMnapi.plugin", "Plugins.Extensions.DMnapi.DMnapi")
				try:
					os.rename(f, f + '.backup')
					open(f, 'w').write(FBp)
					print " DMnapi reenabled - fixed old code."
				except:
					print " but problem with writting ;("
			elif FB.find("DMnapi") > 0:
				print "- skipped - DMnapi support already installed."
			else:
				FBp = patch(f, FB)
				if FBp != FB:
					print "- patched successfully -",
					try:
						os.rename(f, f + '.backup')
						open(f, 'w').write(FBp)
						print " DMnapi enabled."
					except:
						print " but problem with writting ;("

					if d == 'DreamExplorer':
						saveDEkeymap()
						print "\t keymap.xml saved."
				else:
					print "- problem, I didn't recognize the file - DMnapi not enabled."
		else:
			print d, "\n\tnot found."
	print

	os.chmod("/usr/lib/enigma2/python/Plugins/Extensions/DMnapi/dmnapi.py", 0555)

	import urllib2
	latest = re.search('Latest version: .strong.(1[2-9]\..*)..strong', urllib2.urlopen('http://code.google.com/p/dmnapi/wiki/DMnapi').read(),re.MULTILINE ).group(1)
	print "Your DMnapi version: %s, latest is: %s " % ( VERSION, latest)
	if VERSION != latest:
		print "\n\nUpgrade plugin to latest edition\n"
		url = "http://dmnapi.googlecode.com/files/enigma2-plugin-extensions-dmnapi_%s_all.ipk" % latest
		cmd = "cd /tmp ; rm -f enigma2-plugin-extensions-dmnapi*.ipk ; opkg update ; wget -c %s ; ls -al enigma2-plugin-extensions-dmnapi*.ipk ; opkg install enigma2-plugin-extensions-dmnapi*.ipk" % url
		print cmd
		os.system(cmd)
print "Finished."
